﻿using BBICMS.WeiXin;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Xml;

namespace BBICMS.WebUI.Areas.Weixin.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Weixin/Home/

        [HttpGet]
        public ContentResult Index(string echostr, string signature, string timestamp, string nonce)
        {
            if (CheckSignature(signature, timestamp, nonce))
            {
                return Content(echostr);
            }
            return Content("");
        }

        [HttpPost]
        public ContentResult Index()
        {
            StreamReader stream = new StreamReader(Request.InputStream);
            string weixinXML = stream.ReadToEnd();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(weixinXML);

            XmlNodeList list = doc.GetElementsByTagName("xml");
            XmlNode xn = list[0];
            string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;
            string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;

            string MsgType = xn.SelectSingleNode("//MsgType").InnerText;

            string content = "", menu = "";
            if (MsgType.Equals("event"))
            {
                content = "欢迎你的加入！";
            }
            else
            {
                content = xn.SelectSingleNode("//Content").InnerText;
            }

            //content就是你要回复的内容，此间的业务逻辑可以自己定义


            string strresponse = "<xml>";
            strresponse = strresponse + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
            strresponse = strresponse + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
            strresponse = strresponse + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
            strresponse = strresponse + "<MsgType><![CDATA[text]]></MsgType>";
            strresponse = strresponse + "<Content><![CDATA[" + content + menu + "]]></Content>";
            strresponse = strresponse + "<FuncFlag>0<FuncFlag>";
            strresponse = strresponse + "</xml>";

            //   Response.Write(strresponse);


            return Content(strresponse);
        }

        /// <summary>  
        /// 处理微信发来的请求   
        /// </summary>  
        /// <param name="xml"></param>  
        public void processRequest(String xml)
        {

        }

        public bool CheckSignature(string signature, string timestamp, string nonce)
        {
            string token = "heiniaozhi";

            string[] tmpArr = { token, timestamp, nonce };
            Array.Sort(tmpArr);     //字典排序

            string tmpStr = string.Join("", tmpArr);

            tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
            tmpStr = tmpStr.ToLower();
            if (tmpStr == signature)
            {
                return true;
            }

            else
            {
                return false;
            }
        }

        public ActionResult Main()
        {
            JSSDK jssdk = new JSSDK("wx97827a71dea1c6f5","05b6564e093c2f0e259fcd932608a9ac");

            Hashtable  SignPackage = jssdk.getSignPackage();

            ViewBag.timestamp = SignPackage["appId"];
            ViewBag.nonceStr = SignPackage["nonceStr"];
            ViewBag.timestamp = SignPackage["timestamp"];
            ViewBag.url = SignPackage["url"];
            ViewBag.signature = SignPackage["signature"];
            ViewBag.rawString = SignPackage["rawString"];

            return View();
        }
       

    }
}
